package com.xvjialing.lytech.weatherreport.service;

import com.alibaba.fastjson.JSON;
import com.xvjialing.lytech.weatherreport.bean.CityData;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;

@Service
public class CityDataServiceImpl implements CityDataService{

    @Override
    public List<CityData> getCityDataList(){
        List<CityData> cityDataList=null;
        try {
            //读取json文件
            Resource resource = new ClassPathResource("data/cityCode.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"utf-8"));
            StringBuffer buffer=new StringBuffer();
            String line="";
            while ((line=br.readLine())!=null){
                buffer.append(line);
            }
            cityDataList= JSON.parseArray(buffer.toString(),CityData.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return cityDataList;
    }
}